package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.MyDynamicPresenter;
import com.example.peibei.ui.activity.DynamicDetailActivityTwo;
import com.example.peibei.ui.adapter.DynamicListAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyDynamicFragment extends WDFragment {
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    private String mid;
    List<Dynamic.RecordsDTO> dynamics;
    private DynamicListAdapter dynamicListAdapter;
    private MyDynamicPresenter dynamicPresenter;
    private SPUtils spUtils;
    private String token;

    public MyDynamicFragment(String mid) {
        this.mid = mid;
    }

    @Override
    public String getPageName() {
        return "我发的单fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_dynamic_list;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        dynamics = new ArrayList<>();
        order_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        dynamicListAdapter = new DynamicListAdapter(dynamics,"0");
        order_list.setAdapter(dynamicListAdapter);
        order_list.setEmptyView(empty_view);
        //获取数据
        dynamicPresenter = new MyDynamicPresenter(new OrderCall());
        dynamicPresenter.reqeust(true, mid,"Bearer " + token);
        dynamicListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Dynamic.RecordsDTO data = (Dynamic.RecordsDTO) adapter.getData().get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("subjectId", data.getId()+"");
                bundle.putString("status","0");
                intent(DynamicDetailActivityTwo.class, bundle);
            }
        });
    }

    class OrderCall implements DataCall<Dynamic> {

        @Override
        public void success(Dynamic data, Object... args) {
            //添加列表并刷新
            if (dynamicPresenter.getPage() == 1) {
                dynamics.clear();
            }
            dynamicListAdapter.setNewInstance(data.getRecords());
            dynamicListAdapter.notifyDataSetChanged();
            dynamicListAdapter.setOnItemClickListener((adapter, view, position) -> {
                Dynamic.RecordsDTO item = (Dynamic.RecordsDTO) adapter.getData().get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("subjectId", item.getId()+"");
                bundle.putString("status","0");
                intent(DynamicDetailActivityTwo.class, bundle);
            });
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
